package cn.com.sd.system.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;

import cn.com.sd.common.util.MyStringUtils;
import cn.com.sd.common.util.Page;
import cn.com.sd.system.model.Organization;
import cn.com.sd.system.service.IOrganizationService;


public class OrganizationAction {

	HttpServletRequest request = ServletActionContext.getRequest();
	
	@Resource(name = "organizationService")
	private IOrganizationService organizationService;
	
	private Organization organization;
	
	private Integer page;
	
	public String show() {
		
		int count = organizationService.getOrganizationCount();
		Page page = new Page(count, this.page);
		List<Organization> organizations = organizationService.list(page);
		
		request.setAttribute("organizationList", organizations);
		request.setAttribute("page", page);
		return "show";
	}
	
	public String showAdd() {
		return "showAdd";
	}
	
	public String add() {
		
		organization.setCreateTime(System.currentTimeMillis());
		organization.setStatus(0);
		boolean flag = organizationService.saveOrUpdate(organization);
		if (!flag) {
			request.setAttribute(WebCommon.ERR_MSG, "添加失败");
			return "showAdd";
		}
		return show();
	}
	
	public String del() {
		String ids = request.getParameter("ids");
		if (StringUtils.isEmpty(ids)) {
			return "error";
		}
		
		List<Integer> idList = new ArrayList<Integer>();
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			if (StringUtils.isEmpty(id) || MyStringUtils.translateStringToInteger(id) == -1) {
				continue;
			}
			idList.add(MyStringUtils.translateStringToInteger(id));
		}
		if (CollectionUtils.isEmpty(idList)) {
			//MyStringUtils.outputJSONResult("1110", "没有选中记录", null, response);
			return "error";
		}
		boolean result = true;
		if (idList.size() == 1) {
			result = organizationService.del(idList.get(0));
		} else {
			result = organizationService.delBanch(idList);
		}
		if (!result) {
			request.setAttribute(WebCommon.ERR_MSG, "删除失败");
		}
		return show();
		//MyStringUtils.outputJSONResult("8888", "删除成功", null, response);
	}
	
	public String showSet() {
		String id = request.getParameter("id");
		if (StringUtils.isEmpty(id) || MyStringUtils.translateStringToInteger(id) <= 0) {
			return "error";
		}
		Organization organization = organizationService.detail(MyStringUtils.translateStringToInteger(id));
		request.setAttribute("organization", organization);
		
		return "showSet";
	}
	
	public String set() {
		String id = request.getParameter("id");
		if (StringUtils.isEmpty(id) || MyStringUtils.translateStringToInteger(id) == -1) {
			request.setAttribute(WebCommon.ERR_MSG, "更改失败");
			return "showSet";
		}
		boolean flag = organizationService.saveOrUpdate(organization);
		if (!flag) {
			request.setAttribute(WebCommon.ERR_MSG, "更改失败");
			return "showSet";
		}
		return show();
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Organization getOrganization() {
		return organization;
	}

	public void setOrganization(Organization organization) {
		this.organization = organization;
	}
}

